/**
 * Created by AMD on 29.10.14.
 */
package {

import dragonBones.animation.WorldClock;


import flash.geom.Matrix;
import flash.geom.Point;

import starling.display.Image;

import starling.display.QuadBatch;
import starling.display.Sprite;
import starling.events.Event;

public class ShopView extends Sprite {
    private var _shopDesignQuad:QuadBatch = new QuadBatch();

    private var _coordinator:Sprite = new Sprite();
    private var _quadrantWidth:Number = 0;
    public function  ShopView() {
        var _floorImage:Image = GraphicFactory.getImageByName("ShopItems/Floors/Floor1");;
        this._quadrantWidth = _floorImage.width-1;

        var _matrix:Matrix = new Matrix();
        _matrix.rotate(Math.PI/4)
        _matrix.scale(1,0.5);
        _coordinator.transformationMatrix = _matrix;
        //_coordinator.y -= 3; //correct coordinator
        this.addChild(_coordinator);

    }



    public function updateAsQuad(floorData:Vector.<Vector.<String>>,wallDataX:Vector.<String>,wallDataY:Vector.<String>,wallCoverDataX:Vector.<String>,wallCoverDataY:Vector.<String>):void{
        this._updateShopQuads( floorData, wallDataX, wallDataY, wallCoverDataX, wallCoverDataY);
        this._rebuildShopQuad(_shopDesignQuad)
        this.addChild(_shopDesignQuad);
    }

    private static var _matrix_floor:Matrix;
    private static var _matrix_wall1:Matrix;
    private static var _matrix_wall2:Matrix;
    private var _floorQuad:QuadBatch = new QuadBatch();
    private var _wallQuad1:QuadBatch = new QuadBatch();
    private var _wallQuad2:QuadBatch = new QuadBatch();
    private var _floorImageBuf:Image = null;
    private function _updateShopQuads(floorData:Vector.<Vector.<String>>,wallDataX:Vector.<String>,wallDataY:Vector.<String>,wallCoverDataX:Vector.<String>,wallCoverDataY:Vector.<String>): void {
        var sizeX:int = floorData.length;
        var sizeY:int = floorData[0].length;

        var x:int = 0;
        var y:int = 0;


        //Original floorImage Width = 101

        this._floorQuad.reset();
        var _floorQuadrant:Point = new Point();
        for(y = 0; y < sizeY; y++){
            for(x = 0; x<sizeX; x++){
                _floorQuadrant.setTo(x,y)
                this._updateFloorQuadAt(_floorQuadrant,floorData[x][y]);
            }
        }
        if(_matrix_floor == null){
            _matrix_floor = new Matrix();
            _matrix_floor.rotate(Math.PI/4)
            _matrix_floor.scale(1,0.5);
        }
        _floorQuad.transformationMatrix = _matrix_floor;

        //-------------------
        //Walls & Shadows X Side
        this.updateWallQuadSide("x",wallDataX, wallCoverDataX);
        if(_matrix_wall1 == null){
            _matrix_wall1 = new Matrix();
            _matrix_wall1.b = 0.5;
        }
        this._wallQuad1.transformationMatrix = _matrix_wall1;

        //-------------------
        //Walls & Shadows Y Side
        this.updateWallQuadSide("y",wallDataY, wallCoverDataY);
        if(_matrix_wall2 == null){
            _matrix_wall2 = new Matrix();
            _matrix_wall2.b = -0.5;
        }
        this._wallQuad2.transformationMatrix = _matrix_wall2;

    }

    public function updateWallQuadSide(side:String, wallDataSide:Vector.<String>,wallCoverDataSide:Vector.<String>):void {
        switch (side.toLowerCase()){
            case "x":
                this._updateWallQuad(this._wallQuad1,"x",wallDataSide, wallCoverDataSide);
                break;
            case "y":
                this._updateWallQuad(this._wallQuad2,"y",wallDataSide,wallCoverDataSide);
                break;
        }
        this._rebuildShopQuad(_shopDesignQuad);
    }
    public function updateFloorQuadAt(quadrant:Point, floorSpriteName:String):void {
        this._updateFloorQuadAt(quadrant,floorSpriteName);
        this._rebuildShopQuad(_shopDesignQuad);
    }
    private function _updateFloorQuadAt(quadrant:Point, floorSpriteName:String):void {
        this._floorImageBuf = GraphicFactory.getImageForRender(floorSpriteName);
        this._floorImageBuf.x = quadrant.x*(this._floorImageBuf.width-1);
        this._floorImageBuf.y = quadrant.y*(this._floorImageBuf.height-1);
        this._floorQuad.addImage(_floorImageBuf);
    }
    private function _updateWallQuad(_qbBuf:QuadBatch, orient:String, wallData:Vector.<String>, wallCoverData:Vector.<String>):void {
        _qbBuf.reset();
        var size:int = wallData.length;
        var _bufStr:String = "";
        var _doorFlag:Boolean = false;

        var _wallImage:Image = GraphicFactory.getImageForRender("ShopItems/Walls/Wall1");

        var _wallCoverImage:Image = null;
        var _shadowImage:Image = GraphicFactory.getImageForRender("ShopItems/Shadows/Shadow");
        var i:int = 0;
        var _currentWallPosition:Number = 0;

        for(i=0;i<size;i++){
            _doorFlag = false;
            _wallCoverImage = null;

            //_currentWallPosition = ( _wallImage.pivotX+((orient == "x")?-1:(-_wallImageWidth+1)) ) + ((orient == "x")?1:-1)*(i*(_floorImageWidth-1)*Math.cos(Math.PI/4));

            if(orient == "x"){
                _currentWallPosition = i*100*Math.cos(Math.PI/4);
            } else {
                _currentWallPosition = -71 - i*100*Math.cos(Math.PI/4);
            }


            if(wallData[i] == null){//it is DOOR
                _doorFlag = true;
            } else {        // it is WALL
                _wallImage = GraphicFactory.getImageForRender(wallData[i]);
            }
            //more width to close all mini gaps
//            _wallImage.width += 0.1;
//            _wallImage.pivotX += 0.05

            _bufStr = wallCoverData[i];
            if(_bufStr!=null){
                _wallCoverImage = GraphicFactory.getImageForRender(wallCoverData[i]);
            }

            if(!_doorFlag){
                _wallImage.x = _currentWallPosition;
                _qbBuf.addImage(_wallImage);
                if(_wallCoverImage){
                    _wallCoverImage.x = _wallImage.x;
                    _qbBuf.addImage(_wallCoverImage);
                }
            }
            _shadowImage.x = _currentWallPosition;
            _shadowImage.width = 72.4;
            _qbBuf.addImage(_shadowImage);
        }
    }
    private function _rebuildShopQuad(qb:QuadBatch):void {
        qb.reset();
        qb.addQuadBatch(this._floorQuad);
        qb.addQuadBatch(this._wallQuad1);
        qb.addQuadBatch(this._wallQuad2);
    }



    public function getIsometricCoords(screenCoords:Point):Point{
        return this._coordinator.globalToLocal(screenCoords);
    }


    public function getQuadrantToPoint(screenCoords:Point, result:Point):void{
       // var _bufCoords:Point = this.getIsometricCoords(screenCoords);//result = _bufCoords
        this._coordinator.globalToLocal(screenCoords,result);

        var _bufNumber:int = int(result.x/_quadrantWidth);
        var _xQuadrant:int = _bufNumber-((result.x>0)?0:1);
        _bufNumber = int(result.y/_quadrantWidth);
        var _yQuadrant:int = _bufNumber-((result.y>0)?0:1);

        result.setTo(_xQuadrant,_yQuadrant);
    }

    public function getQuadrant(screenCoords:Point):Point{
        var _bufCoords:Point = this.getIsometricCoords(screenCoords);
        var _bufNumber:int = int(_bufCoords.x/_quadrantWidth);
        var _xQuadrant:int = _bufNumber-((_bufCoords.x>0)?0:1);
        _bufNumber = int(_bufCoords.y/_quadrantWidth);
        var _yQuadrant:int = _bufNumber-((_bufCoords.y>0)?0:1);

        return new Point(_xQuadrant,_yQuadrant);
    }

    public function quadrantToIsometricToPoint(quadrant:Point, result:Point):void{
        var _x:Number = (quadrant.x + 0.5) * _quadrantWidth;
        var _y:Number = (quadrant.y + 0.5) * _quadrantWidth;

        result.setTo(_x,_y);
    }

    public function quadrantToIsometric(quadrant:Point):Point{
        var result:Point = new Point();
        this.quadrantToIsometricToPoint(quadrant,result);
        return result;
    }


    public function isometricToScreen(isometricCoords:Point):Point{
        return this._coordinator.localToGlobal(isometricCoords);
    }


    public static function get matrix_wall1():Matrix {
        return _matrix_wall1;
    }

    public static function get matrix_wall2():Matrix {
        return _matrix_wall2;
    }
}
}
